package com.qingmu.club.minaapi.custom.logic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingmu.club.minaapi.plus.entity.Club;
import com.qingmu.club.minaapi.plus.entity.ClubShop;
import com.qingmu.club.minaapi.plus.entity.ClubUsers;
import com.qingmu.club.minaapi.plus.service.ClubService;
import com.qingmu.club.minaapi.plus.service.ClubShopService;
import com.qingmu.club.minaapi.utils.AddressUtil;
import com.qingmu.club.minaapi.utils.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ClubShopLogic {

    @Autowired
    ClubShopService clubShopService;

    @Autowired
    ClubStaffShopLogic clubStaffShopLogic;

    @Autowired
    ClubService clubService;

    @Autowired
    ClubUsersLogic clubUsersLogic;

    /**
     * 查询用户附近门店
     * @param latitude
     * @param longitude
     * @param page
     * @param pageSize
     * @param userId
     * @return
     */
    public HashMap<String,Object> queryNearByShop(String shopName,Double latitude,Double longitude,Integer page,Integer pageSize,String userId) {

        if (page == null) {
            page = 0;
        }
        if (pageSize == null) {
            pageSize = 10;
        }

        QueryWrapper<Club> clubQueryWrapper = new QueryWrapper<>();
        clubQueryWrapper.eq(Club.STATUS, 0);
        List<Club> clubList = this.clubService.list(clubQueryWrapper);
        String[] clubIds = new String[clubList.size()];
        for (int i = 0; i < clubList.size(); i++) {
            clubIds[i] = clubList.get(i).getId();
        }


        List<ClubShop> clubShopList = this.clubUsersLogic.queryUserAttentionShop(userId);
        String[] clubShopIds = new String[clubShopList.size()];
        for (int i = 0; i < clubShopList.size(); i++) {
            clubShopIds[i] = clubShopList.get(i).getId();
        }

        Map<String, Double> map = AddressUtil.getDistance(latitude, longitude, 3000); //默认附近100公里的门店

        QueryWrapper<ClubShop> qw = new QueryWrapper<>();
        // 按距离排序
        String sql = "order by (%s-latitude)*(%s-latitude)+(%s-longitude)*(%s-longitude) asc";
        qw.eq(ClubShop.STATUS, 1);
        qw.notIn(clubIds.length != 0, ClubShop.CLUB_ID, clubIds);
        qw.last(String.format(sql, latitude, latitude, longitude, longitude));
        qw.between(ClubShop.LONGITUDE, map.get("minLon"), map.get("maxLon"));
        qw.between(ClubShop.LATITUDE, map.get("minLat"), map.get("maxLat"));
        qw.notIn(clubShopIds.length!=0,ClubShop.ID, clubShopIds);
        qw.like(!StringUtils.isEmpty(shopName), ClubShop.SHOP_NAME, shopName);

        IPage<ClubShop> list = clubShopService.page(new Page<>(page, pageSize), qw);

        HashMap<String, Object> returnMap = new HashMap<>();
        List<HashMap<String, Object>> clubShops = new ArrayList<>();
        if (CollectionUtils.isEmpty(list.getRecords())) {
            return returnMap;
        }
        for (ClubShop clubShop : list.getRecords()) {
            String distance = AddressUtil.calDistance(longitude, latitude, clubShop.getLongitude(), clubShop.getLatitude());//距离
            int coachSum = clubStaffShopLogic.queryCoachSum(clubShop.getId()); //教练总数
            int fansSum = clubUsersLogic.queryUserSum(clubShop.getId());//粉丝总数
            HashMap<String, Object> clubShopMap = ConvertUtil.objectToMap(clubShop);
            clubShopMap.put("coachSum", coachSum);
            clubShopMap.put("distance", distance);
            clubShopMap.put("fansSum", fansSum);
            clubShops.add(clubShopMap);
        }

        List<HashMap<String, Object>> attentionShopMap = new ArrayList<>();
        List<ClubShop> attentionShopList = this.clubUsersLogic.queryUserAttentionShop(userId);
        for (ClubShop clubShop : attentionShopList) {
            String distance = AddressUtil.calDistance(longitude, latitude, clubShop.getLongitude(), clubShop.getLatitude());//距离
            HashMap<String, Object> clubShopMap = ConvertUtil.objectToMap(clubShop);
            clubShopMap.put("distance", distance);
            attentionShopMap.add(clubShopMap);
        }
        returnMap.put("attentionShopList", attentionShopMap);
        returnMap.put("clubShopList", clubShops);
        returnMap.put("current", list.getCurrent());
        returnMap.put("total", list.getTotal());
        returnMap.put("pages", list.getPages());
        returnMap.put("size", list.getSize());

        return returnMap;
    }


}
